<?php

namespace App\Http\Middleware;

use App\Services\Com\StateService;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use App\Jobs\Exposure;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (!$request->expectsJson())
        {
            return route('login');
        }
    }

    public function terminate($request, $response){
        // 编写业务逻辑
        
        // Log::info('经过了后置中间件');
        $route = Route::current();
        $name = Route::currentRouteName();
        $action = Route::currentRouteAction();
        $arr_1 = json_decode(json_encode($route), true);
        Log::info($arr_1['uri']);
        $res = (array)$response->getData();
        $req = $request->all();
        $reqArr = json_decode(json_encode($req), true);
        $arr = json_decode(json_encode($res), true);

        if( strpos($arr_1['uri'], 'api/admin') === false && env('LOG_DEBUG') != false ){
            Log::info($arr_1['uri']);
            Log::info(var_export($reqArr, true));
            Log::info(var_export($arr, true));
        }
        
        if($arr['isSuccess'] == true){
            //判断当前接口url
            switch ($arr_1['uri']) {
                //feeds
                case 'api/client/square/feeds':
                        foreach ($arr['returnData']['list'] as $key => $value) {
                            //曝光统计
                            $exposure['type'] = 'feed';
                            $exposure['uuids'] = $value['info']['uuid'];
                            \App\Jobs\Exposure::dispatch($exposure)->onQueue('Exposure');
                            
                        }            
                    break;
                //学校feeds
                case 'api/client/square/school':
                        foreach ($arr['returnData']['list'] as $key => $value) {
                            //曝光统计
                            $exposure['type'] = 'feed';
                            $exposure['uuids'] = $value['info']['uuid'];
                            \App\Jobs\Exposure::dispatch($exposure)->onQueue('Exposure');
                            
                        }
                    break;
                //用户圈子feeds
                case 'api/client/square/member_feeds':
                        foreach ($arr['returnData']['list'] as $key => $value) {
                            //曝光统计
                            $exposure['type'] = 'feed';
                            $exposure['uuids'] = $value['info']['uuid'];
                            \App\Jobs\Exposure::dispatch($exposure)->onQueue('Exposure');
                            
                        }
                    break;
                //用户评论发送消息
                case 'api/client/member/service/comment':
                    Log::info('用户评论发送消息');
                    $reqArr['type'] = 0;// 评论
                    if(!empty($reqArr['service_uuid']) && empty($reqArr['parent_uuid'])){
                        StateService::eventSendMessage($reqArr);
                    }
                    // 给父级发送评论通知
                    if(!empty($reqArr['parent_uuid'])){
                        $newReqParams = $reqArr;
                        $newReqParams['place'] =  $reqArr['type'] = 0;// 评论对方的评论
                        $newReqParams['service_uuid'] = $reqArr['parent_uuid'];
                        Log::info('给父级发送评论通知', $newReqParams);
                        StateService::eventSendMessage($newReqParams);
                    }


                    break;
                //用户点赞发送消息
                case 'api/client/member/service/like':
                    //delete 0是点赞成功 1是删除点赞
                    if(isset($arr['returnData']['delete']) && $arr['returnData']['delete'] == 0){
                        $reqArr['type'] = 1;// 点赞
                        StateService::eventSendMessage($reqArr);
                    }
                    break;
                //用户关注发送消息。
                /*case 'api/client/member/service/follow':
                    $reqArr['type'] = 3;// 关注
                    StateService::eventSendMessage($reqArr);
                    break;*/
                //活动报名发送消息
                /*case 'api/client/activity/join':
                    $reqArr['type'] = 5;// 消息类型
                    $reqArr['place'] = 3;// 业务类型
                    $reqArr['service_uuid'] = $reqArr['activity_uuid'];// 活动uuid
                    StateService::eventSendMessage($reqArr);
                    // 给自己发消息 myself_uuid
                    // $reqArr['type'] = 9;// 消息类型
                    // $reqArr['myself_uuid'] = 1;// 发给自己一条
                    // StateService::eventSendMessage($reqArr);
                    break;*/
                default:
                    // code...
                    break;
            }
            
        }
        
        

    }
}
